home *** CD-ROM | disk | FTP | other *** search
- //************************************************************************
- //
- // Module: echoc.c
- //
- // Purpose:
- // ECHOC.EXE demonstrates the PowerTCP C DLL Interface for TCP
- // This has been compiled in 16 and 32 bit versions
- // This project accomplished on 30 Jul 94
- // Listens on port 7 for a connection and echos back all data received
- // If a second connection occurs, the first is terminated
- //
- //************************************************************************
- //
- // Written by Dart Communication Application Programming Group.
- // Copyright (c) 1994 Dart Communications. All Rights Reserved.
- //
- //************************************************************************
-
- #define STRICT // be bold!
- #pragma warning (disable:4100 4355 4699)
-
- #include <windows.h>
- #include <string.h>
-
- #include "..\..\include\powertcp.h"
- #include "echo.h"
- #include "echoc.hh"
-
- HPOWERTCP hListener = NULL; // session identifier for listener
- HPOWERTCP hSession = NULL; // session identifier for echo-er
- HINSTANCE hInstance = NULL;
- HWND hWnd; // handle of main window
- char * Class="PowerEcho";
- HICON hIcon;
-
- int PASCAL WinMain( HINSTANCE hNewInstance, HINSTANCE hPrevInstance,
- LPSTR lpszCmdLine, int nCmdShow )
- {
- MSG msg ;
-
- hInstance=hNewInstance; // init for later
-
- if (!hPrevInstance)
- if (!InitApplication())
- return ( FALSE ) ;
-
- if (!InitInstance( nCmdShow ))
- return ( FALSE ) ;
-
- while (GetMessage( &msg, NULL, 0, 0 ))
- {
- {
- TranslateMessage( &msg ) ;
- DispatchMessage( &msg ) ;
- }
- }
-
- if (UnregisterClass(Class,hInstance) && hIcon)
- DestroyIcon(hIcon);
-
- return ( (int) msg.wParam ) ;
-
- } // end of WinMain()
-
- BOOL InitApplication(void)
- {
- WNDCLASS wndclass ;
-
- // register window class
-
- wndclass.style = 0 ;
- wndclass.lpfnWndProc = WndProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance ;
- hIcon=wndclass.hIcon = LoadIcon( hInstance, "PowerEcho" );
- wndclass.hCursor = LoadCursor( NULL, IDC_ARROW ) ;
- wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
- wndclass.lpszMenuName = MAKEINTRESOURCE( TTYMENU ) ;
- wndclass.lpszClassName = Class ;
-
- return( RegisterClass( &wndclass ) ) ;
-
- } // end of InitApplication()
-
- void Display (LPCSTR Message)
- {
- // put Message into display box
- HWND hEditWnd=GetDlgItem(hWnd,EDITWND);
- if (IsWindow(hEditWnd))
- {
- //SendMessage(hEditWnd,EM_SETSEL,0,MAKELONG(32000,32000));
- SendMessage(hEditWnd,EM_REPLACESEL,0,(LPARAM)Message);
- SendMessage(hEditWnd,EM_REPLACESEL,0,(LPARAM)(LPSTR)"\r\n");
- //SendMessage(hEditWnd,EM_SETSEL,0,MAKELONG(0,0));
- }
- }
-
-
-
- // all PowerTCP callback functions go here so we don't have to prototype
- // them...
-
- void CALLBACK ConnectEvent(
- HPOWERTCP hPowerTcp, /* identifies session notifying our app */
- DWORD UserData, /* UserData from Connect() function */
- LPCSTR RemoteDotAddr, /* remote host connected to in dot notation */
- WORD RemotePort,/* remote port connected to */
- LPCSTR LocalDotAddr, /* local host address in dot notation */
- WORD LocalPort, /* local port */
- LPCSTR LocalName /* name of default local host */
- )
- {
- char Buf[100];
- unsigned int Port=(unsigned int)RemotePort;
- // if connected to someone else, kill it
- if (hSession)
- {
- Display("Closing Echo Connect to accept a new one...");
- CloseTcp(hSession,TRUE); // abort
- }
-
- hSession=hPowerTcp;
- Display("Connection information:");
- wsprintf(Buf," Remote host %s on remote port %u connected to %s",
- RemoteDotAddr, Port, LocalName);
- Display(Buf);
- Port=(unsigned int)LocalPort;
- wsprintf(Buf," on our host %s local port %u",LocalDotAddr,Port);
- Display(Buf);
- }
-
- void CALLBACK RecvEvent(
- HPOWERTCP hPowerTcp, /* identifies session notifying our app */
- DWORD UserData, /* UserData from Open() function */
- LPBYTE Data, /* data from remote source */
- size_t Cnt /* byte count for RecvData */
- )
- {
- if (!Data)
- if (hPowerTcp==hListener)
- Display("Listener has closed");
- else
- Display("Echo session has closed");
- else
- // echo data
- SendTcp(hSession,Data,Cnt,FALSE,0);
- }
-
- void CALLBACK SendEvent(
- HPOWERTCP hPowerTcp, /* identifies session notifying our app */
- DWORD UserData, /* UserData from Open() function */
- DWORD SendInstance /* SendInstance from Send() functions */
- )
- {
- Display ("Echo Sent Successfully!");
- }
-
- void CALLBACK ListenEvent(
- HPOWERTCP hPowerTcp, /* identifies session notifying our app */
- DWORD UserData, /* UserData from Connect() function */
- LPCSTR LocalDotAddr, /* local host address in dot notation */
- WORD LocalPort, /* local port */
- LPCSTR LocalName /* name of default local host */
- )
- {
- // called, so we are listening...
- char Buf[100];
- unsigned int Port=(unsigned int)LocalPort;
- hListener=hPowerTcp;
- wsprintf(Buf,"Listening on %s port %u (%s)",LocalDotAddr,Port,LocalName);
- Display(Buf);
- }
-
- void CALLBACK ExceptionEvent(
- HPOWERTCP hPowerTcp, /* identifies session notifying our app */
- DWORD UserData, /* UserData from Open() function */
- PT_EXCEPTION ErrorCode, /* spontaneous error notification */
- LPCSTR ErrorDesc /* description of error */
- )
- {
- Display(ErrorDesc);
- }
-
- void CALLBACK AcceptEvent(
- HPOWERTCP hPowerTcp, /* identifies session notifying our app */
- DWORD UserData /* UserData from Listen() function */
- )
- {
- // must create a new session to handle the connection
- AcceptTcp(
- 0ul, /* user-defined data passed back by all callback functions */
- NULL, /* use license number assigned to you by Dart */
- PT_SHOW,
- hPowerTcp, /* hPowerTcp that generated AcceptEvent() */
- ConnectEvent, /* your event handler for session Open/Close notification */
- RecvEvent, /* your event handler for receiving data */
- SendEvent, /* your event handler for Send confirmation */
- ExceptionEvent); /* your event handler for exception notifications */
- }
-
- // main window procedure
-
- LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg,
- WPARAM wParam, LPARAM lParam )
- {
- switch (uMsg)
- {
- case WM_CREATE:
- {
- // set Gcp220
- // create an edit window
- HWND hEdit=CreateWindow("edit",
- NULL, // no window name
- WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_VSCROLL|ES_READONLY,
- 0,0,0,0,
- hWnd,
- (HMENU)EDITWND, // my child ID
- hInstance,
- NULL);
-
- break;
- }
-
- case WM_COMMAND:
- {
- switch ((WORD) wParam)
- {
- case IDM_ABOUT:
- DialogBoxParam( hInstance, MAKEINTRESOURCE( ABOUTDLGBOX ), hWnd, AboutDlgProc, 0l ) ;
- break;
-
- case IDM_EXIT:
- PostMessage( hWnd, WM_CLOSE, 0, 0L ) ;
- break ;
- }
- }
- break ;
-
- case WM_SIZE:
- MoveWindow(GetDlgItem(hWnd,EDITWND),0,0,LOWORD(lParam),HIWORD(lParam),TRUE);
- break ;
-
- case WM_DESTROY:
- PostQuitMessage( 0 ) ;
- break ;
-
- case WM_CLOSE:
- if (hSession)
- CloseTcp(hSession,TRUE); // abort
- if (hListener)
- CloseTcp(hListener,TRUE); // abort
- DialogBoxParam( hInstance, MAKEINTRESOURCE( ABOUTDLGBOX ), hWnd, AboutDlgProc, 0l ) ;
- // fall through
-
- default:
- return( DefWindowProc( hWnd, uMsg, wParam, lParam ) ) ;
- }
- return 0L ;
-
- } // end of WndProc()
-
- BOOL InitInstance( int nCmdShow )
- {
- // listen for duration of program
- //FARPROC test=MakeProcInstance((FARPROC)ListenEvent,hInstance);
-
- // create the TTY window
- hWnd = CreateWindow( Class, "PowerTCP ECHO Demo",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, hInstance, NULL ) ;
-
- if (!hWnd)
- return ( FALSE ) ;
-
-
- ListenTcp(
- 0ul, // no user-data saved
- NULL, // contact Dart for your LicenseKey
- PT_SHOW,
- NULL, // local address is default local host
- 7, // listen on the well-known echo port)
- ListenEvent, // our callback procedure
- RecvEvent,
- AcceptEvent, // our callback for accepted connections
- ExceptionEvent); // for exceptions
-
-
- ShowWindow( hWnd, nCmdShow ) ;
- UpdateWindow( hWnd ) ;
-
- return ( TRUE ) ;
-
- } // end of InitInstance()
-
-
- BOOL CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg,
- WPARAM wParam, LPARAM lParam )
- {
- switch (uMsg)
- {
- case WM_INITDIALOG:
- {
-
- #ifdef ABOUTDLG_USEBITMAP
- // if we are using the bitmap, hide the icon
-
- ShowWindow( GetDlgItem( hDlg, IDD_ABOUTICON ), SW_HIDE ) ;
- #endif
-
- }
- return ( TRUE ) ;
-
- #ifdef ABOUTDLG_USEBITMAP
- // used to paint the bitmap
-
- case WM_PAINT:
- {
- HBITMAP hBitMap ;
- HDC hDC, hMemDC ;
- PAINTSTRUCT ps ;
-
- // load bitmap and display it
-
- hDC = BeginPaint( hDlg, &ps ) ;
- if (NULL != (hMemDC = CreateCompatibleDC( hDC )))
- {
- hBitMap = LoadBitmap( hInstance,
- MAKEINTRESOURCE( TTYBITMAP ) ) ;
- hBitMap = (HBITMAP)SelectObject( hMemDC, hBitMap ) ;
- BitBlt( hDC, 10, 10, 64, 64, hMemDC, 0, 0, SRCCOPY ) ;
- DeleteObject( SelectObject( hMemDC, hBitMap ) ) ;
- DeleteDC( hMemDC ) ;
- }
- EndPaint( hDlg, &ps ) ;
- }
- break ;
- #endif
-
- case WM_COMMAND:
- if ((WORD) wParam == IDD_OK)
- {
- EndDialog( hDlg, TRUE ) ;
- return ( TRUE ) ;
- }
- break;
- }
- return ( FALSE ) ;
-
- } // end of AboutDlgProc()
-